package com.zx.wangyixinwen.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.zx.wangyixinwen.MyRequestQueue;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.bean.Topic;

import java.util.List;

/**
 * Created by dllo on 16/1/19.
 */
public class TopicAdapter extends BaseAdapter {
    private Context context;
    private List<Topic> datas;

    public TopicAdapter(Context context, List<Topic> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return null == datas ? 0 : datas.get(0).getData().getExpertList().size();
    }

    @Override
    public Object getItem(int position) {
        return datas.size() > 0 && datas != null ? datas.size() : 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        myViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_topic, null);
            holder = new myViewHolder();
            holder.image_background = (NetworkImageView) convertView.findViewById(R.id.iv_item_topic_background);
            holder.image_name = (NetworkImageView) convertView.findViewById(R.id.iv_item_topic_touxiang);
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_item_topic_name);
            holder.tv_body = (TextView) convertView.findViewById(R.id.tv_item_topic_body);
            convertView.setTag(holder);
        } else {
            holder = (myViewHolder) convertView.getTag();
        }
        MyRequestQueue.setNetImage(holder.image_background, datas.get(0).getData().getExpertList().get(position).getPicurl());
        holder.image_name.setImageResource(R.mipmap.refresh);
        MyRequestQueue.setNetImage(holder.image_name, datas.get(0).getData().getExpertList().get(position).getHeadpicurl());
        holder.tv_name.setText(datas.get(0).getData().getExpertList().get(position).getName());
        holder.tv_body.setText(datas.get(0).getData().getExpertList().get(position).getAlias());
        return convertView;
    }

    public class myViewHolder {
        private NetworkImageView image_background, image_name;
        private TextView tv_name, tv_body;
    }
}
